package cn.usian.pricingstrategy.date.service.impl;

import cn.usian.pojo.pricingstrategy.date.PricingSpecialDate;
import cn.usian.pricingstrategy.date.mapper.DateMapper;
import cn.usian.pricingstrategy.date.service.IDateService;
import cn.usian.vo.pricingstrategy.date.PricingSpecialDateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DateServiceImpl implements IDateService {
    @Autowired
    private DateMapper dateMapper;

    @Override
    public List<PricingSpecialDateVo> list(String year) {
        log.info("year:{}", year);

        // 使用 DateTimeFormatter 替代 SimpleDateFormat
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        List<PricingSpecialDate> list = dateMapper.list(year);
        List<PricingSpecialDateVo> dateVos = list.stream()
                .map(item -> {
                    LocalDate specialDate = item.getSpecialDate(); // 直接获取 LocalDate
                    PricingSpecialDateVo vo = new PricingSpecialDateVo();
                    vo.setSpecialDate(specialDate == null ? null : formatter.format(specialDate));
                    return vo;
                })
                .collect(Collectors.toList());

        // 增强日志记录信息
        // list.forEach(item -> log.info("item: {}", item.toString()));

        log.info("list size: {}", list.size());
        return dateVos;
    }

    @Override
    public void save(PricingSpecialDate date) {
        dateMapper.save(date);
    }

    @Override
    public void delete(PricingSpecialDate date) {
        LocalDate specialDate = date.getSpecialDate();

        // 直接使用 LocalDate，无需再转换回 Date 类型
        date.setSpecialDate(specialDate);

        // 删除操作
        dateMapper.delete(date);
    }
}
